//===--- tgmath.swift.gyb -------------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

%{

# Don't need 64-bit (Double/CDouble) overlays. The ordinary C imports work fine.
# FIXME: need 80-bit (Float80/long double) versions when long double is imported
overlayFloatBits = [32] # 80
allFloatBits = [32, 64] # 80

def floatName(bits):
    if bits == 32:
        return 'Float'
    if bits == 64:
        return 'Double'
    if bits == 80:
        return 'Float80'

def cFloatName(bits):
    if bits == 32:
        return 'CFloat'
    if bits == 64:
        return 'CDouble'
    if bits == 80:
        return 'CLongDouble'

def cFuncSuffix(bits):
    if bits == 32:
        return 'f'
    if bits == 64:
        return ''
    if bits == 80:
        return 'l'

# Each of the following lists is ordered to match math.h

# (T) -> T
# These functions do not have a corresponding LLVM intrinsic
UnaryFunctions = ['acos', 'asin', 'atan', 'tan',
                  'acosh', 'asinh', 'atanh', 'cosh', 'sinh', 'tanh',
                  'expm1',
                  'log1p', 'logb',
                  'cbrt', 'sqrt', 'erf', 'erfc', 'tgamma',
                  ]

# These functions have a corresponding LLVM intrinsic
# We call this intrinsic via the Builtin method so keep this list in
# sync with core/BuiltinMath.swift.gyb
UnaryIntrinsicFunctions = ['cos', 'sin',
                  'exp', 'exp2',
                  'log', 'log10', 'log2',
                  'fabs',
                  'ceil', 'floor', 'nearbyint', 'rint', 'round', 'trunc',
                  ]

# (T, T) -> T
BinaryFunctions = ['atan2', 'hypot', 'pow', 'fmod', 
                   'remainder', 'copysign', 'nextafter', 'fdim', 'fmax', 'fmin']

# These functions have special implementations.
OtherFunctions = ['fpclassify', 
                  'isnormal', 'isfinite', 'isinf', 'isnan', 'signbit', 
                  'modf', 'ldexp', 'frexp', 'ilogb', 'scalbn', 'lgamma', 
                  'remquo', 'nan', 'fma', 
                  'jn', 'yn']

# These functions are imported correctly as-is.             
OkayFunctions = ['j0', 'j1', 'y0', 'y1']

# These functions are not supported for various reasons.
UnhandledFunctions = ['math_errhandling', 'scalbln', 
                      'lrint', 'lround', 'llrint', 'llround', 'nexttoward', 
                      'isgreater', 'isgreaterequal', 'isless', 'islessequal', 
                      'islessgreater', 'isunordered', '__exp10', 
                      '__sincos', '__cospi', '__sinpi', '__tanpi', '__sincospi']


def AllFloatTypes():
    for bits in allFloatBits:
        yield floatName(bits), cFloatName(bits), cFuncSuffix(bits)

def OverlayFloatTypes():
    for bits in overlayFloatBits:
        yield floatName(bits), cFloatName(bits), cFuncSuffix(bits)

def TypedUnaryFunctions():
    for ufunc in UnaryFunctions:
        for bits in overlayFloatBits:
            yield floatName(bits), cFloatName(bits), cFuncSuffix(bits), ufunc

def TypedUnaryIntrinsicFunctions():
    for ufunc in UnaryIntrinsicFunctions:
        for bits in allFloatBits:
            yield floatName(bits), ufunc

def TypedBinaryFunctions():
    for bfunc in BinaryFunctions:
        for bits in overlayFloatBits:
            yield floatName(bits), cFloatName(bits), cFuncSuffix(bits), bfunc

}%


// Unary functions
// Note these do not have a corresponding LLVM intrinsic
% for T, CT, f, ufunc in TypedUnaryFunctions():
@_transparent
@warn_unused_result
public func ${ufunc}(x: ${T}) -> ${T} {
  return ${T}(${ufunc}${f}(${CT}(x)))
}

% end

// Unary intrinsic functions
// Note these have a corresponding LLVM intrinsic
% for T, ufunc in TypedUnaryIntrinsicFunctions():
@_transparent
@warn_unused_result
public func ${ufunc}(x: ${T}) -> ${T} {
  return _${ufunc}(x)
}

% end

// Binary functions

% for T, CT, f, bfunc in TypedBinaryFunctions():
@_transparent
@warn_unused_result
public func ${bfunc}(lhs: ${T}, _ rhs: ${T}) -> ${T} {
  return ${T}(${bfunc}${f}(${CT}(lhs), ${CT}(rhs)))
}

% end

// Other functions

% # These are AllFloatTypes not OverlayFloatTypes because of the Int return.
% for T, CT, f in AllFloatTypes():
%   if f == '':
%     f = 'd'
%   end
@_transparent
@warn_unused_result
public func fpclassify(x: ${T}) -> Int {
  return Int(__fpclassify${f}(${CT}(x)))
}

% end

% # These are AllFloatTypes not OverlayFloatTypes because we need to cover 
% # them all because C's declarations are compiler builtins.
% for T, CT, f in AllFloatTypes():
@_transparent
@warn_unused_result
public func isnormal(value: ${T}) -> Bool {
  return value.isNormal
}

@_transparent
@warn_unused_result
public func isfinite(value: ${T}) -> Bool {
  return value.isFinite
}

@_transparent
@warn_unused_result
public func isinf(value: ${T}) -> Bool {
  return value.isInfinite
}

@_transparent
@warn_unused_result
public func isnan(value: ${T}) -> Bool {
  return value.isNaN
}

@_transparent
@warn_unused_result
public func signbit(value: ${T}) -> Int {
  return value.isSignMinus ? 1 : 0
}

% end

% # These are AllFloatTypes not OverlayFloatTypes because of the tuple return.
% for T, CT, f in AllFloatTypes():
@_transparent
@warn_unused_result
public func modf(value: ${T}) -> (${T}, ${T}) {
  var ipart = ${CT}(0)
  let fpart = modf${f}(${CT}(value), &ipart)
  return (${T}(ipart), ${T}(fpart))
}

% end

% # This is AllFloatTypes not OverlayFloatTypes because of the Int parameter.
% for T, CT, f in AllFloatTypes():
@_transparent
@warn_unused_result
public func ldexp(x: ${T}, _ n: Int) -> ${T} {
  return ${T}(ldexp${f}(${CT}(x), CInt(n)))
}

% end

% # This is AllFloatTypes not OverlayFloatTypes because of the tuple return.
% for T, CT, f in AllFloatTypes():
@_transparent
@warn_unused_result
public func frexp(value: ${T}) -> (${T}, Int) {
  var exp = CInt(0)
  let frac = frexp${f}(${CT}(value), &exp)
  return (${T}(frac), Int(exp))
}

% end

% # This would be AllFloatTypes not OverlayFloatTypes because of the Int return.
% # ... except we need an _silgen_name to avoid an overload ambiguity.
% for T, CT, f in OverlayFloatTypes():
@_transparent
@warn_unused_result
public func ilogb(x: ${T}) -> Int {
  return Int(ilogb${f}(${CT}(x)))
}

% end

@warn_unused_result
@_silgen_name("ilogb") 
func _swift_Darwin_ilogb(value: CDouble) -> CInt

@_transparent
@warn_unused_result
public func ilogb(x: Double) -> Int {
  return Int(_swift_Darwin_ilogb(CDouble(x)))
}


% # This is AllFloatTypes not OverlayFloatTypes because of the Int parameter.
% for T, CT, f in AllFloatTypes():
@_transparent
@warn_unused_result
public func scalbn(x: ${T}, _ n: Int) -> ${T} {
  return ${T}(scalbn${f}(${CT}(x), CInt(n)))
}

% end

% # This is AllFloatTypes not OverlayFloatTypes because of the tuple return.
% for T, CT, f in AllFloatTypes():
% # The real lgamma_r is not imported because it hides behind macro _REENTRANT.
@warn_unused_result
@_silgen_name("lgamma${f}_r") 
func _swift_Darwin_lgamma${f}_r(_: ${CT},
                                _: UnsafeMutablePointer<CInt>) -> ${CT}
@_transparent
@warn_unused_result
public func lgamma(x: ${T}) -> (${T}, Int) {
  var sign = CInt(0)
  let value = withUnsafeMutablePointer(&sign) { 
    (signp: UnsafeMutablePointer<CInt>) -> ${CT} in
    return _swift_Darwin_lgamma${f}_r(${CT}(x), signp)
  }
  return (${T}(value), Int(sign))
}

% end

% # This is AllFloatTypes not OverlayFloatTypes because of the tuple return.
% for T, CT, f in AllFloatTypes():
@_transparent
@warn_unused_result
public func remquo(x: ${T}, _ y: ${T}) -> (${T}, Int) {
  var quo = CInt(0)
  let rem = remquo${f}(${CT}(x), ${CT}(y), &quo)
  return (${T}(rem), Int(quo))
}

% end

% for T, CT, f in OverlayFloatTypes():
@_transparent
@warn_unused_result
public func nan(tag: String) -> ${T} {
  return ${T}(nan${f}(tag))
}

% end

% for T, CT, f in OverlayFloatTypes():
@_transparent
@warn_unused_result
public func fma(x: ${T}, _ y: ${T}, _ z: ${T}) -> ${T} {
  return ${T}(fma${f}(${CT}(x), ${CT}(y), ${CT}(z)))
}

% end

% # These C functions only support double. The overlay fixes the Int parameter.
@_transparent
@warn_unused_result
public func jn(n: Int, _ x: Double) -> Double {
  return jn(CInt(n), x)
}

@_transparent
@warn_unused_result
public func yn(n: Int, _ x: Double) -> Double {
  return yn(CInt(n), x)
}

% end

// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End:
